﻿using CXW.Core.Sharp.Gui;
using System;
using System.Windows.Interop;

namespace CXW.Core.Sharp
{
    public class WorkbenchStartup
    {
        public void InitializeWorkbench()
        {
            ComponentDispatcher.ThreadIdle -= ComponentDispatcher_ThreadIdle; // ensure we don't register twice
            ComponentDispatcher.ThreadIdle += ComponentDispatcher_ThreadIdle;
            WorkbenchSingleton.InitializeWorkbench(new WorkBenchWindow());
        }

        private void ComponentDispatcher_ThreadIdle(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.RaiseIdle(e);
        }
        /// <remark/>
        public void Run()
        {

            System.Windows.Forms.Application.Run(WorkbenchSingleton.Workbench);

            try
            {
                PropertyService.Set("Workbench", WorkbenchSingleton.Workbench.CretatMemento());
                PropertyService.Set("DockPanel", WorkbenchSingleton.Workbench.CretatDockPanel());
            }
            catch (Exception e)
            {
                MessageService.ShowException(e, "Exception while saving workbench state.");
            }
        }
    }
}
